﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CourseRecommend
{
    public partial class LoginForm : Form
    {
        public String loginName;
        public String password;
        private string pw;
        public LoginForm()
        {
          
            InitializeComponent();
            
        }
        public void logMeIn()
        {
            string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Catalog.mdf;Integrated Security=True;User Instance=True";
            SqlConnection myConnection = new SqlConnection(connectionString);

            try
            {
                myConnection.Open();
                MessageBox.Show("connected");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

            loginName = UsernameTextBox.Text;
            pw = PasswordTextBox.Text;
            password = RegisterForm.HashPassword(pw);
            // password = pw;
            SqlCommand command = new SqlCommand("SELECT * FROM [LoginDataTable] WHERE StudentID =  @loginName AND Password = @password", myConnection);
            command.Parameters.Add("@loginName", loginName);
            command.Parameters.Add("@password", password);
            SqlDataReader reader = null;
            reader = command.ExecuteReader();

            while (reader.Read())
            {
                if (String.Compare(reader["StudentID"].ToString(), loginName) == 0 && String.Compare(reader["Password"].ToString(), password) == 0)
                {
                    Form HomeForm = new HomeForm();
                    HomeForm.Show();
                    HomeForm.Activate();
                    this.Hide();
                }
                else
                    MessageBox.Show("Incorrect username and or password");

            }
            if (! reader.HasRows)
            {
                MessageBox.Show("Incorrect username and or password");

            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void LoginButton_Click(object sender, EventArgs e)
        {
            logMeIn();
        }

        private void UsernameTextBox_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {

        }

        private void PasswordTextBox_TextChanged(object sender, EventArgs e)
        {
            password = PasswordTextBox.Text;
        }

        private void RegisterButton_Click(object sender, EventArgs e)
        {
            Form RegisterForm = new RegisterForm();
            RegisterForm.Show();
            RegisterForm.Activate();
            this.Hide();

        }
    }
}
